﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Services.Exceptions;
using HiTeachers.Utilities;

namespace HiTeachers.Services
{
    public class TeacherServices
    {
        //private static readonly ITeacherDao TeacherDao = NHibernateDaoFactory.Instance.GetDao<ITeacherDao>("teacher");
        private static readonly ITeacherDao TeacherDao = NHibernateDaoFactory.Instance.GetTeacherDao();

        #region Adjustment Methods
        public static Teacher SaveOrUpdate(Teacher teacher)
        {
            if (Guid.Empty.Equals(teacher.ID))
            {
                if (LoggerEmailServices.IsExistEmail(teacher.Email, EmailType.Lock))
                    throw new EmailAlreadyLockException(string.Format("{0} is already lock!", teacher.Email));

                if (TeacherDao.IsEmailAlreadyExist(teacher.Email) != null)
                    throw new EmailAlreadyExistException(string.Format("{0} is already exist!", teacher.Email));

                TeacherDao.Save(teacher);
            }
            else
                TeacherDao.SaveOrUpdate(teacher);

            return teacher;
        }

        public static void Delete(Guid itemId)
        {
            Teacher teacher = TeacherDao.GetById(itemId, false);
            TeacherDao.Delete(teacher);
        }

        public static void Delete(Teacher teacher)
        {
            TeacherDao.Delete(teacher);
        }
        #endregion

        public static Teacher IsEmailAlreadyExist(string emailAddress)
        {
            return TeacherDao.IsEmailAlreadyExist(emailAddress);
        }

        public static Teacher GetById(Guid itemId)
        {
            return TeacherDao.GetById(itemId, false);
        }

        public static IList<Teacher> GetNewestTeachers(int maxResult)
        {
            return TeacherDao.GetNewestTeachers(maxResult);
        }

        public static int TotalActivateTeachers()
        {
            return Convert.ToInt32(TeacherDao.TotalActivateTeachers());
        }

        public static IList<Teacher> GetActivateTeachers(int maxResult, int startIndex)
        {
            return TeacherDao.GetActivateTeachers(maxResult, startIndex);
        }

        public static IList<Teacher> GetSubcribeTeachers(string subjects)
        {
            return TeacherDao.GetSubcribeTeachers(subjects);
        }

        #region Selection Methods
        public static int TotalNumberOfTeacher(int accountStatus)
        {
            return Convert.ToInt32(TeacherDao.TotalNumberOfTeacher(accountStatus));
        }

        public static IList<Teacher> GetTeachers(int accountStatus)
        {
            return TeacherDao.GetTeachers(accountStatus);
        }

        public static IList<Teacher> GetTeachers(int accountStatus, int maximumRows, int startRowIndex)
        {
            return TeacherDao.GetTeachers(accountStatus, maximumRows, startRowIndex);
        }

        public static void UpdateTeacher(Guid ID)
        {
            return;
        }
        #endregion

        #region MemberShip Methods
        public static Teacher GetUser(string username)
        {
            return TeacherDao.GetUser(username);
        }

        public static Teacher GetUser(string username, string password)
        {
            password = CryptorEngine.Encrypt(password, CryptorEngine.EncryptKey, true);
            return TeacherDao.GetUser(username, password);
        }
        #endregion

        #region Need to Notification
        public static IList<Teacher> GetRemindMembers()
        {
            return TeacherDao.GetRemindMembers();
        }

        public static IList<Teacher> GetExpiredMembers()
        {
            return TeacherDao.GetExpiedMembers();
        }
        #endregion
    }
}
